﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace SYFranework
{
    public class UIManager : Singelon<UIManager>
    {
        /// <summary>
        /// 需要打开UI界面的时候保存的参数类
        /// </summary>
        class UIInfoData
        {
            public UIType UType
            { get;
              private  set;
            }
            public string Path { get; private set; }
            public object[] UIParams { get; private set; }

            public UIInfoData(UIType _uType,string _path,object[] _uiParams)
            {
                UType = _uType;
                Path = _path;
                UIParams = _uiParams;
            }
        }

        // 保存当前已经打开的UI集合
        private Dictionary<UIType, GameObject> dicOpenUIs = null;

        // 将要打开的UI合集
        private Stack<UIInfoData> stackUIs = null;
        public override void Init()
        {
            dicOpenUIs = new Dictionary<UIType, GameObject>();
            stackUIs = new Stack<UIInfoData>();
        }

       /// <summary>
       /// 获取UI
       /// </summary>
       /// <typeparam name="T"></typeparam>
       /// <param name="_uiType"></param>
       /// <returns></returns>
        public T GetUI<T>(UIType _uiType)  where T : BaseUI
        {
            GameObject _retObj = GetUIObject(_uiType);
         
                if (_retObj != null)
                {
                    return _retObj.GetComponent<T>();
                }
            return null;
        }

        /// <summary>
        /// 获取UI身上的对象
        /// </summary>
        /// <param name="_uiType"></param>
        /// <returns></returns>
        public GameObject GetUIObject(UIType _uiType)
        {
            GameObject _retObj = null;
            if (!dicOpenUIs.TryGetValue(_uiType, out _retObj))
            {
                throw new Exception("_UI字典里面获取UI失败");
            }
            return _retObj;
        }


        public void OpenUI(UIType _uiType)
        {
            UIType[] _uiTypes = new UIType[1];
            _uiTypes[0] = _uiType;
            OpenUI(false, _uiTypes, null);
        }

        public void OpenUI(UIType _uiType,params object[] _uiParames)
        {
            UIType[] _uiTypes = new UIType[1];
            _uiTypes[0] = _uiType;
            OpenUI(false, _uiTypes, _uiParames);
        }
        public void OpenUICloseOthers(UIType _uiType)
        {
            UIType[] _uiTypes = new UIType[1];
            _uiTypes[0] = _uiType;
            OpenUI(true, _uiTypes, null);
        }
        public void OpenUICloseOthers(UIType _uiType,params object[] _uiParames)
        {
            UIType[] _uiTypes = new UIType[1];
            _uiTypes[0] = _uiType;
            OpenUI(true, _uiTypes, _uiParames);
        }
        /// <summary>
        /// 打开UI界面
        /// </summary>
        /// <param name="_isCloseOthers"></param>
        /// <param name="_uiTypes"></param>
        /// <param name="_uiparams"></param>
        public void OpenUI(bool _isCloseOthers,UIType[] _uiTypes, params object[] _uiParams)
        {
            // 关闭UI逻辑
            if (_isCloseOthers)
            {

            }
            // 往栈空间压入UI界面
            for (int i = 0; i < _uiTypes.Length; i++)
            {
                UIType _uiType = _uiTypes[i];
                if (!dicOpenUIs.ContainsKey(_uiType))
                {
                    // 根据UIType类型拿到路径
                    string _path = UIPathDefines.GetUIPrefabsPathByTyte(_uiType);
                    stackUIs.Push(new UIInfoData(_uiType,_path, _uiParams));
                }
            }
            // UI加载
            if (stackUIs.Count > 0)
            {

            }
        }
    }
      
}
